package com.crm.system.controller.productManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.productManager.CustomerProduct;
import com.crm.model.qo.productManager.CustomerProductQO;
import com.crm.model.qo.productManager.CustomerProductUpdateQO;
import com.crm.model.vo.productManager.AnalyseFinishOrderProductVO;
import com.crm.model.vo.productManager.CustomerProductUpdateVO;
import com.crm.model.vo.productManager.CustomerProductVO;
import com.crm.model.vo.productManager.ProductAnalyseVO;
import com.crm.system.service.productManager.CustomerProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yincuimei
 * @Date 2021/1/27 15:55
 * 产品管理
 */
@RestController
@RequestMapping("/customerProduct")
@Slf4j
public class CustomerProductController {
    @Autowired
    private CustomerProductService customerProductService;

    /**
     * 产品管理--非会员产品列表
     * @return
     */
    @PostMapping("/selectCustomerProductList")
    public DataResponse selectCustomerProductList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                  @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                  @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                  @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                  @RequestParam(value="tag",required = false) Integer tag,
                                                  CustomerProductQO customerProductQO//查询条件
    ){
        try{
            DhPageInfo<CustomerProductVO> customerProductVODhPageInfo = customerProductService.selectCustomerProductVOList(customerProductQO,pageIndex,pageSize,sortName,sortOrder,tag);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerProductVODhPageInfo);
        }catch(Exception e){
            log.error("customerProduct selectCustomerProductList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 产品管理--根据产品ID查询产品信息
     * @param customerProductId  ---产品ID
     * @return
     */
    @PostMapping("/selectCustomerProductUpdateInfo")
    public DataResponse selectCustomerProductUpdateInfo(@RequestParam(value="customerProductId")String customerProductId){
        try{
            CustomerProductUpdateVO customerProductUpdateVO = customerProductService.selectCustomerProductUpdateVOById(customerProductId);
            if(customerProductUpdateVO != null){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerProductUpdateVO);
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }catch(Exception e){
            log.error("customerProduct selectCustomerProductUpdateInfo error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     *产品列表--上/下架操作
     * @return
     */
    @PostMapping("/updateShelfStatus")
    public DataResponse updatePoolState(@RequestParam(value="customerProductId")String customerProductId,     //产品ID
                                        @RequestParam(value="shelfStatus",required = false)String shelfStatus //产品状态:0-待定；1-已上架； 2-已下架
    ){
        try{
            Boolean result = customerProductService.updateCustomerProductShelfStatus(customerProductId,shelfStatus);
            if(result){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }catch(Exception e){
            log.error("customerProduct updateShelfStatus error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 产品管理--新增时校验产品名称是否重复
     * @param productName   ---产品名称
     * @return
     */
    @PostMapping("/isRepeatProductName")
    public DataResponse isRepeatProductName(@RequestParam(value="productName")String productName){
        try{
            Boolean result = customerProductService.isExistProductName(productName);
            if(result){//产品名称已存在
                return new DhResponse(ResponseCodeEnum.CUSTOMER_PRODUCT_NAME_REPEAT.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());//产品名称不存在,可以添加
        }catch(Exception e){
            log.error("customerProduct isRepeatProductName error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 产品管理--新增时校验产品编码是否重复
     * @param productCode   ---产品编码
     * @return
     */
    @PostMapping("/isRepeatProductCode")
    public DataResponse isRepeatProductCode(@RequestParam(value="productCode")String productCode){
        try{
            Boolean result = customerProductService.isExistProductCode(productCode);
            if(result){//产品编码已存在
                return new DhResponse(ResponseCodeEnum.CUSTOMER_PRODUCT_CODE_REPEAT.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());//产品编码不存在,可以添加
        }catch(Exception e){
            log.error("customerProduct isRepeatProductCode error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 产品管理--新增时校验专题ID是否重复
     * @param topicId   ---专题ID
     * @return
     */
    @PostMapping("/isRepeatTopicId")
    public DataResponse isRepeatTopicId(@RequestParam(value="topicId")Integer topicId) {
        try {
            Boolean result = customerProductService.isExistTopicId(topicId);
            if (result) {//专题ID已存在
                return new DhResponse(ResponseCodeEnum.CUSTOMER_TOPIC_ID_REPEAT.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());//专题ID不存在,可以添加
        } catch (Exception e) {
            log.error("customerProduct isRepeatTopicId error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 产品管理--新增产品
     * @param customerProduct
     * @return
     */
    @PostMapping("/addCustomerProduct")
    public DataResponse addCustomerProduct(@RequestBody CustomerProduct customerProduct){
        try{
           Boolean result = customerProductService.insertCustomerProduct(customerProduct);
           if(result){//新增产品成功
               return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
           }
           return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }catch(Exception e){
            log.error("customerProduct addCustomerProduct error:", e);
           return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 产品管理--编辑产品
     * @param customerProductUpdateQO  ---产品编辑信息
     * @return
     */
    @PostMapping("/updateCustomerProduct")
    public DataResponse updateCustomerProduct(@RequestBody CustomerProductUpdateQO customerProductUpdateQO){
        try{
            Boolean result = customerProductService.updateCustomerProductById(customerProductUpdateQO);
            if(result){//编辑产品成功
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }catch(Exception e){
            log.error("customerProduct updateCustomerProduct error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 活动管理———新增活动-查询活动未选择的产品下拉框列表
     * @return
     */
    @GetMapping("/selectProductDropDownList")
    public DataResponse selectProductDropDownList(@RequestParam(value="productName",required = false) String productName){
        List<HashMap> hashMapList = customerProductService.selectProductDropDownList(productName);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),hashMapList);
    }


    /**
     * 产品分析
     * @Author chaifuyou
     * @return
     */
    @GetMapping("/selectProductAnalyse")
    public DataResponse selectProductAnalyse(@RequestParam(value="dateType") String dateType){
        try {
            ProductAnalyseVO resultVo = customerProductService.selectProductAnalyse(dateType);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),resultVo);
        }catch (Exception e){
            log.error("customerProduct selectProductAnalyse error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 订单完成的产品列表统计
     * @Author chaifuyou
     * @return
     */
    @GetMapping("/selectFinishOrderProductList")
    public DataResponse selectFinishOrderProductList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                             @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                             @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                             @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                             @RequestParam(value="dateType") String dateType){
        try {
            DhPageInfo<AnalyseFinishOrderProductVO> resultVo = customerProductService.selectFinishOrderProductList(pageIndex,pageSize,sortName,sortOrder,dateType);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),resultVo);
        }catch (Exception e){
            log.error("customerProduct selectProductAnalyse error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }


    /**
     * 产品管理--非会员产品列表
     * @return
     */
    @PostMapping("/selectCustomerProductListForCoupon")
    public DataResponse selectCustomerProductListForCoupon(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                  @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                  @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                  @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                  @RequestParam(value="tag",required = false) Integer tag,
                                                  CustomerProductQO customerProductQO//查询条件
    ){
        try{
            DhPageInfo<Map<String,Object>> customerProductVODhPageInfo = customerProductService.selectCustomerProductVOListForCoupon(customerProductQO,pageIndex,pageSize,sortName,sortOrder,tag);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerProductVODhPageInfo);
        }catch(Exception e){
            log.error("customerProduct selectCustomerProductListForCoupon error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }
}
